package com.speciality.demo.demos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.speciality.demo.demos.entity.User;
import com.speciality.demo.demos.mapper.UserMapper;
import com.speciality.demo.demos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import com.speciality.demo.demos.result.MD5;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Async
    @Override
    public void add(User user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        user.setCreatedAt(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public User login(User user) {
        //获取传递来的密码
        String passwd = user.getPassword();
        //将密码加密
       String password = MD5.encrypt(passwd);

        //条件构造器查询
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        System.out.println(password);
        System.out.println(user.getUsername());
        wrapper.eq("password",password);
        System.out.println("登录SQL：" + wrapper.getSqlSegment()); // 打印SQL片段
        User one = userMapper.selectOne(wrapper);
        return one;
    }

    @Override
    public List<User> allUser() {
        QueryWrapper<User> wrapper =new QueryWrapper<>();
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

}
